package com.asm.controller;


import com.alibaba.fastjson.JSONObject;
import com.asm.mapper.SNVTraitsFiltMapper;
import com.asm.pojo.*;
import com.asm.service.*;
import com.asm.util.DatatableResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
public class detailController {
    @Autowired
    private SnvCpgAllService snvCpgAllService;
    @Autowired
    private Snv2GeneService snv2GeneService;
    @Autowired
    private SnvGeneEqtlFiltService snvGeneEqtlFiltService;
    @Autowired
    private CpgGeneFiltService cpgGeneFiltService;
    @Autowired
    private Analysis2With3DService analysis2With3DService;
    @Autowired
    private SNVATACService snvatacService;
    @Autowired
    private Snv2SEService snv2SEService;
    @Autowired
    private SNV2InteractionService snv2InteractionService;
    @Autowired
    private SNV2CTCFInsolutorService snv2CTCFInsolutorService;
    @Autowired
    private CpG2CGIService cpG2CGIService;
    @Autowired
    private SNV2TfCHIPseqService snv2TfCHIPseqService;
    @Autowired
    private SNV2enhancerService snv2enhancerService;
    @Autowired
    private SNVTraitsFiltMapper snvTraitsFiltMapper;

    @GetMapping("/detail/Introduction")
    public DatatableResult<SnvCpgAll> snvCpgAllList(@RequestParam("asmid") String asmid){
        DatatableResult<SnvCpgAll> datatableResult = new DatatableResult<>();
//        使用snv查询
        List<SnvCpgAll> list = snvCpgAllService.getDetailByASMid(asmid);
        PageInfo<SnvCpgAll> pageInfo = new PageInfo<>(list);
        datatableResult.setDraw(datatableResult.getDraw());
        datatableResult.setData(pageInfo.getList());
        datatableResult.setRecordsTotal((int) pageInfo.getTotal());
        datatableResult.setRecordsFiltered(datatableResult.getRecordsTotal());
        return datatableResult;
    }

    @GetMapping("/detail/associatedGenes")
    public JSONObject associatedGenesList(@RequestParam("snv") String snv,
                                          @RequestParam("cpgPos") String cpgPos){
        JSONObject result = new JSONObject();
        String[] cpgList = cpgPos.split(",");
        System.out.println(Arrays.toString(cpgList));
        List<Snv2Gene> ProximalGenes = snv2GeneService.getDetailProximalGenesBySnv(snv);
        List<SnvGeneEqtlFilt> EqtlFilt = snvGeneEqtlFiltService.getDetailEqtlGenesBySnv(snv);
        List<Gene3D> InteractionGenes = analysis2With3DService.getGene3D(snv);
        List<CpgGeneFilt> CpgTargetGenes = cpgGeneFiltService.getDetailCpgTargetGenesByCpg(snv);
        result.put("ProximalGenes",ProximalGenes);
        result.put("EqtlFilt",EqtlFilt);
        result.put("InteractionGenes",InteractionGenes);
        result.put("CpgTargetGenes",CpgTargetGenes);
        return result;
    }

    @GetMapping("/detail/cis_tfbinding")
    public JSONObject CisAndTFbindingList(@RequestParam("snv") String snv,
                                          @RequestParam("cpgPos") String cpgPos,
                                          @RequestParam("chr") String chr){
        JSONObject result = new JSONObject();
        String[] cpg = cpgPos.split(",");
//        for (int i = 0; i < cpg.length; i++) {
//            cpg[i] = chr + ":" + cpg[i];
//        }
        for (String c : cpg) {
            System.out.println("cpgs====>"+c);
        }
        List<SNVATAC> AccessibleChromatin = snvatacService.getDetailAccessibleChromatinBySnv(snv);
        List<Snv2SE> SuperEnhancer = snv2SEService.getDetailSuperEnhancerBySnv(snv);
        List<SNV2Interaction> Interaction = snv2InteractionService.getDetailEnhancerAndInteractionBySnv(snv);
        List<SNV2CTCFInsolutor> Insolutor = snv2CTCFInsolutorService.getDetailInsolutorBySnv(snv);

        List<CpG2CGI> CpgIsland = cpG2CGIService.getDetailCpgIslandByCpGs(snv);
        List<SNV2TfCHIPseq> TFbinding = snv2TfCHIPseqService.getDetailTFbindingBySnv(snv);
        List<SNV2enhancer> enhancers = snv2enhancerService.getDetailEnhancerBySnv(snv);
        result.put("AccessibleChromatin",AccessibleChromatin);
        result.put("SuperEnhancer",SuperEnhancer);
        result.put("enhancers",enhancers);
        result.put("Interaction",Interaction);
        result.put("Insolutor",Insolutor);
        result.put("CpgIsland",CpgIsland);
        result.put("TFbinding",TFbinding);

        return result;
    }

    @GetMapping("/detail/gwas")
    public JSONObject gwasList(@RequestParam("snv") String snv){
        JSONObject result = new JSONObject();
        List<SNVTraitsFilt> gwas = snvTraitsFiltMapper.selectGWASByAsm(snv);
        result.put("gwas",gwas);
        return result;
    }
}
